const {
  Tab,
  extend
} = require('../../dist/index');
var app = getApp();
Page(extend({}, Tab, {
  data: {
    inputShowed: false,
    inputVal: "",
    couponList: [],
    inputContent: "",
    selectCategory: 0,
    sorts: "hot",
    types: "defalut",
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    cate_method: {},
    sort_method: {},
    cate_src: [],
    picWidth: wx.getSystemInfoSync().windowWidth,
    pageIndex: 1,
    menu_cate: [{
      name: "全部",
      id: "0"
    }],    
  },
  onShareAppMessage: function() {

  },
  onShow: function() {
    this.setData({
      isLoading: true,
      loadOver: false,
    });
    if (this.data.theconfig.wxname != undefined) {
      wx.setNavigationBarTitle({
        title: this.data.theconfig.wxname
      });
    }
  },
  onReady: function() {
    //this.setData({ is_kouling: app.globalData.is_kouling})

  },

  onLoad: function(options) {
    // this.getCategoryList()
    this.getcate();
    this.getMoreCouponList();
    //this.getbanner();
    var that = this;
    this.setData({
      theconfig: wx.getStorageSync('theconfig'),
      sort_method: {
        list: [{
          id: 'hot',
          name: '销量'
        }, {
          id: 'new',
          name: '最新'
        }, {
          id: 'rate',
          name: '折扣'
        }, {
          id: 'price',
          name: '价格'
        }],
        selectedId: 'hot'
      },

    })

  },
  //取分类
  getcate: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiurl + "/Home/Item/getCate",
      data: {},
      dataType: 'json',
      success: function(res) {    

        that.setData({
          cate_method: {
            list: that.data.menu_cate.concat(res.data.data),
            //list: res.data.data.cate_list.p,
            selectedId: "0",
            scroll: true,
            height: 42,
          }
        })
        wx.setStorageSync("menu_cate", that.data.cate_method)
      }
    })
  },
  get99: function() {
    wx.switchTab({
      url: "/pages/99/99",
    })
  },
  get20: function() {
    wx.navigateTo({
      url: '/pages/20/20',
    })
  },
  getdeq: function() {
    wx.navigateTo({
      url: '/pages/large/large',
    })
  },
  getjxquan: function() { 
    wx.navigateTo({
      url: '/pages/jxquan/jxquan',
      //  url: '/pages/large/large',
    });
  },
  //取商品
  getMoreCouponList: function() {
    var that = this
    wx.request({
      url: app.globalData.apiurl + "/Home/Item/getItems",
      dataType: 'json',
      data: {
        //"type": that.data.types,
        "sort": that.data.sorts,
        "p": that.data.pageIndex, 
        "size": 10,
        "cid": that.data.selectCategory,
      },     
      success: function(resRequest) {
        // console.log(resRequest.data.data.result);      
        if (resRequest.statusCode === 200) {
          if (resRequest.data.status == "0") {
            that.setData({
              couponList: that.data.couponList.concat(resRequest.data.data.items_list),
              isLoading: false
            })
          } else {
            that.setData({
              isLoading: true,
              loadOver: true
            })
          }
        }
      }
    })
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    if (componentId === "cate_method") {
      this.setData({
        selectCategory: selectedId
      })
    }
    if (componentId === "sort_method") {
      this.setData({
        sorts: selectedId
      })
    }
    this.setData({
      [`${componentId}.selectedId`]: selectedId,
      isLoading: true,
      couponList: [],
      pageIndex: 1,
    });
    this.getMoreCouponList()
  },
  onPullDownRefresh: function() {
    this.setData({
      couponList: [],
      isLoading: true,
      pageIndex: 1
    })
    wx.stopPullDownRefresh()
    this.getMoreCouponList()
  },
  onReachBottom: function() {
    this.setData({
      isLoading: true,
      pageIndex: this.data.pageIndex + 1
    })
    this.getMoreCouponList()
  },
  inputTyping: function() {
    wx.navigateTo({
      url: "../../pages/search/search",
    })
  },
  compare: function(property, sort) {
    return function(a, b) {
      var value1 = a[property];
      var value2 = b[property];
      if (sort == 'jx') {
        return value2 - value1;
      } else {
        return value1 - value2;
      }
    }
  },
  tapBanner: function(e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/detail/detail?gid=" + e.currentTarget.dataset.id
      })
    }
  },


}));